/**
 * Created by ss on 16/4/16.
 */
var $ = require('jquery');
var init = require('../util/init.js');
var util = require('../util/util.js');
var templateUtil = require('../util/templateUtil.js');
var tpl = require('../../tpl/sendTypeChoose.tpl');
require('../../scss/chooseSendType.scss');

init.initNav(true, '添加发件');
var type = parseInt(localStorage.getItem("type"));
var initArray = [];
if( type && type === 1) {
    initArray.push({
        img: "http://d2.freep.cn/3tb_1605142109136msr564060.png",
        type: 1
    });
}
initArray.push({
    img: "http://d2.freep.cn/3tb_160514220818lwds564060.png",
    type: 2
});
$("ul").html(templateUtil.template(tpl, {
    list: initArray
}));
$("li").click(function () {
    $("li").removeClass("on");
    $(this).addClass("on");
});
$("#bottomButton .button").click(function () {
    var sendType = $("li.on").attr("data-type");
    if(!sendType){
        alert("请选择一种寄件方式");
        return false;
    }
    localStorage.setItem("sendType", sendType);
    if(parseInt(sendType)==1){
        util.changePage("./mapHelicopter");
    }else{
        var post = {
            receiver: encodeURIComponent(localStorage.getItem("receiver")),
            sender: encodeURIComponent(localStorage.getItem("sender")),
            type: localStorage.getItem("type"),
            sendType: sendType
        };
        $.post(util.getAjaxUrl("/addSend"), post).then(function (data) {
            if(data.code == 1) {
                localStorage.removeItem("receiver");
                localStorage.removeItem("sender");
                localStorage.removeItem("type");
                localStorage.removeItem("sendType");
                util.changePage("./sendList");
            } else {
                alert(data.errorInfo);
            }
        });
    }

});